package net.wzw.interceptor;

import io.jsonwebtoken.Claims;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import net.wzw.entity.LoginUser;
import net.wzw.enums.BizCodes;
import net.wzw.utils.ApiResult;
import net.wzw.utils.CommonUtil;
import net.wzw.utils.JWTUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author wzw
 * @date 2025/4/8
 * @Description 登录拦截器
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    /*
     * Description: 使用threadLoal
     * @param null
     * @Return: {@link null}
     * @Author: Administrator
     * @Date: 2025/4/8 21:23
     */
    public static ThreadLocal<LoginUser> threadLocal = new ThreadLocal<>();

    /*
     * Description: 登录拦截器，解密JWT
     * @param null
     * @Return: {@link null}
     * @Author: Administrator
     * @Date: 2025/4/8 21:23
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取access_token
        String access_token =  request.getHeader("token");

        if(access_token==null){
            //请求头中没有则从参数中取
            access_token = request.getParameter("token");
        }

        if(StringUtils.isNotBlank(access_token)){

             //解密TOKEN
           Claims claims =  JWTUtil.checkJWT(access_token);

           if(claims==null){

               //解密失败，登陆过期，提示
               CommonUtil.sendResponse(response, ApiResult.doResult(BizCodes.USER_ACCOUNT_UNLOGIN));
               log.info("登录拦截器,解密token失败:{}",ApiResult.doResult(BizCodes.USER_ACCOUNT_UNLOGIN));
               return false;
           }

           //解密成功，获取相关信息
            Long userId = Long.valueOf(claims.get("id").toString());
            String name = (String) claims.get("name");
            String head_avatar = (String) claims.get("head_avatar");
            String mail = (String) claims.get("mail");
            //封装LoginUser对象 优化可使用Builder建造者模式链式调用创建对象
            LoginUser loginUser = new LoginUser();
            loginUser.setId(userId);
            loginUser.setMail(mail);
            loginUser.setName(name);
            loginUser.setHeadAvatar(head_avatar);


            //使用threadLocal 传递登录用户信息
            threadLocal.set(loginUser);
            return  true;

        }
         //TOKEN为空，提示账号未登录
        CommonUtil.sendResponse(response,ApiResult.doResult(BizCodes.USER_ACCOUNT_UNLOGIN));
        log.info("登录拦截器,解密token失败:{}",ApiResult.doResult(BizCodes.USER_ACCOUNT_UNLOGIN));
        return  false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
